package com.taotao.search.service;

import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.search.pojo.SearchResult;
@Service
public class SearchServiceImpl implements SearchService {
	@Autowired
	private SearchDao searchDao;
	@Override
	public SearchResult getItemsInfo(String query, int indexPage, int rows) {
		SolrQuery solrQuery=new SolrQuery();
		solrQuery.setQuery(query);
		solrQuery.setStart((indexPage-1)*rows);
		solrQuery.setRows(rows);
		//设置默认搜索域
		solrQuery.set("df", "item_keywords");
		solrQuery.setHighlight(true);
		solrQuery.addHighlightField("item_title");
		solrQuery.setHighlightSimplePre("<em style=\"color:red\">");
		solrQuery.setHighlightSimplePost("</em>");
		SearchResult searchResult = searchDao.getItems(solrQuery);
		long countTotal=searchResult.getCountTotal();
		long countPage=countTotal/rows;
		if(countTotal%rows>0){
			countPage++;
		}
		searchResult.setCountPage(countPage);
		searchResult.setCurPage(indexPage);
		return searchResult;
	}

}
